package com.ruoyi.web.controller.system;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.Course;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.system.service.impl.SysCourseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/system/course")
public class SysCourseController extends BaseController {
    private String prefix = "system/course";
    public static int id = 0; // 全局变量
    public static String course1 ="";


    @Autowired
    private SysCourseService sysCourseService;

    @RequiresPermissions("system:course:view")
    @GetMapping()
    public String user() {
        return prefix + "/post";
    }

    @RequiresPermissions("system:course:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Course course) {
        startPage();
        List<Course> list = sysCourseService.selectCourseList(course);
        return getDataTable(list);
    }

//    @RequiresPermissions("system:course:edit")
//    @GetMapping("/edit/{id}")
//    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
//        mmap.put("course", sysCourseService.selectcourseById(id));
//        return prefix + "/edit";
//    }

    @GetMapping("/avatar/{id}")
    public String avatar(@PathVariable("id") Integer id,ModelMap mmap)
    {mmap.put("course",sysCourseService.selectcourseById(id)) ;
     this.id=id;
    // this.course1=(String) mmap.get(course1);
        return prefix + "/avatar";
    }
    @PostMapping("/avatar")
    @ResponseBody
    public AjaxResult saveSysCourse(@Validated Course  course,@RequestParam("coursepicturePath") MultipartFile coursepicture) {
        course.setId(this.id);
        course.setCourse1(this.course1);
        try {
            if (coursepicture != null && !coursepicture.isEmpty()) {
                // 使用 FileUploadUtils 处理文件上传并获取文件路径
                String filePath = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), coursepicture, MimeTypeUtils.IMAGE_EXTENSION);

                // 打印文件路径进行调试
                log.info("Uploaded file path: {}", filePath);

                if (filePath.isEmpty()) {
                    return AjaxResult.error("文件上传失败");
                }
                // 设置课程图片路径
                course.setCoursepicture(filePath);
            }
            if (sysCourseService.updateCoursePicture(course)>0) {
                return AjaxResult.success();
            } else {
                return AjaxResult.error("更新课程失败");
            }
        } catch (Exception e) {
            log.error("修改课程图片失败！", e);
            return AjaxResult.error(e.getMessage());
        }
    }

    @RequiresPermissions("system:course:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        mmap.put("course",sysCourseService.selectcourseById(id)) ;
        return prefix + "/edit";
    }
    @RequiresPermissions("system:course:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult saveSysCourse(@Valid Course course) {
        sysCourseService.updateCourse(course);
        return toAjax(1);
    }
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    @RequiresPermissions("system:course:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSysCourse(@Validated Course course) {
        sysCourseService.insertCourse(course);
        return toAjax(1);
    }

    @RequiresPermissions("system:course:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(@Valid Integer[] ids) {
        sysCourseService.deleteCourseByIds(ids);
        return toAjax(1);
    }
}
